/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Giaodien;
import conn.JavaConnectDb;
import java.sql.Connection;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
import net.proteanit.sql.DbUtils;

import btnhom.Loainentdc;

/**
 * @author nhung
 */

public class LoaiNen extends javax.swing.JFrame {

    /**
     * Creates new form LoaiNen
     */
    Connection conn = JavaConnectDb.ConnectDb();
    OraclePreparedStatement pst;
    OracleResultSet rs;
    
    private int flag;
    public LoaiNen() {
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void loadLoaiNen()
    {
         try{
             flag=0;
             txtMaloai.setEnabled(true);
             txtMota.setEditable(true);
             OracleResultSet rs = new Loainentdc().selectLoaiNen();
             tbLoaiNen.setModel(DbUtils.resultSetToTableModel(rs));
    
             btThem.setVisible(true);
             btXoa.setVisible(true);
             btSua.setVisible(true);
             btThoat.setVisible(true);
             btLuu.setVisible(true);
             btHuy.setVisible(true);
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        } 
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtMaloai = new javax.swing.JTextField();
        txtMota = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbLoaiNen = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btThem = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btSua = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        btLuu = new javax.swing.JButton();
        btHuy = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setText("THONG TIN VE LOAI NEN");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 255)));
        jPanel1.setForeground(new java.awt.Color(51, 255, 51));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Ma Loai Nen");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Mo ta");

        txtMaloai.setName("txtMaloai"); // NOI18N

        txtMota.setName("txtMota"); // NOI18N

        tbLoaiNen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tbLoaiNen.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        tbLoaiNen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ma_Loai_Nen", "Mo Ta"
            }
        ));
        tbLoaiNen.setName("tbLoaiNen"); // NOI18N
        tbLoaiNen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoaiNenMouseClicked(evt);
            }
        });
        tbLoaiNen.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tbLoaiNenPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(tbLoaiNen);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMaloai)
                    .addComponent(txtMota, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtMaloai)
                        .addGap(3, 3, 3)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMota, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 0, 204)));
        jPanel2.setForeground(new java.awt.Color(240, 240, 240));

        btThem.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btThem.setForeground(new java.awt.Color(0, 0, 255));
        btThem.setText("Them");
        btThem.setToolTipText("");
        btThem.setName("btThem"); // NOI18N
        btThem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btThemMouseClicked(evt);
            }
        });
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        btXoa.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btXoa.setForeground(new java.awt.Color(0, 0, 255));
        btXoa.setText("Xoa");
        btXoa.setName("btXoa"); // NOI18N
        btXoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btXoaMouseClicked(evt);
            }
        });

        btSua.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btSua.setForeground(new java.awt.Color(0, 0, 255));
        btSua.setText("Sua");
        btSua.setName("btSua"); // NOI18N
        btSua.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSuaMouseClicked(evt);
            }
        });

        btThoat.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btThoat.setForeground(new java.awt.Color(0, 0, 255));
        btThoat.setText("Thoat");
        btThoat.setName("btThoat"); // NOI18N
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        btLuu.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btLuu.setForeground(new java.awt.Color(0, 0, 255));
        btLuu.setText("Luu");
        btLuu.setName("btLuu"); // NOI18N
        btLuu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btLuuMouseClicked(evt);
            }
        });

        btHuy.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        btHuy.setForeground(new java.awt.Color(0, 0, 255));
        btHuy.setText("Huy");
        btHuy.setName("btHuy"); // NOI18N
        btHuy.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btHuyMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btSua, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btThoat)
                .addGap(18, 18, 18)
                .addComponent(btLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btHuy, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btHuy)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btLuu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btSua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(47, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(174, 174, 174))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btThemActionPerformed

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        dispose();
        new Sub_main_QLTDC().setVisible(true);
    }//GEN-LAST:event_btThoatActionPerformed

    private void btThemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btThemMouseClicked
        flag=1;
        txtMaloai.setText(null);
        txtMota.setText(null);
        btThem.setVisible(true);
        btXoa.setVisible(true);
        btSua.setVisible(true);
        btThoat.setVisible(true);
        btLuu.setVisible(true);
        btHuy.setVisible(true);
        txtMaloai.setEnabled(true);
      
    }//GEN-LAST:event_btThemMouseClicked

    private void btLuuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btLuuMouseClicked
    if(txtMaloai.getText().equals("") )
    {
     JOptionPane.showMessageDialog(null,"Yeu cau nhap ma loai");
    }
    else if( txtMota.getText().equals(""))
    {
     JOptionPane.showMessageDialog(null, "Yeu cau nhap mo ta"); 
    }
    else {
           try{
               if(flag==1)
               {
                   
                Loainentdc insert = new Loainentdc();
                insert.insertLoaiNen(txtMaloai.getText(), txtMota.getText());
                flag=0;
               }
              if(flag==2)
               {
                Loainentdc update = new Loainentdc();
                update.updateLoaiNen(txtMaloai.getText(), txtMota.getText());
                flag=0;
                }
             }catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Yeu cau nhap day du thong tin");
        }
        }
        loadLoaiNen();
    }//GEN-LAST:event_btLuuMouseClicked

    private void btHuyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btHuyMouseClicked
         loadLoaiNen();
    }//GEN-LAST:event_btHuyMouseClicked

    private void btSuaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSuaMouseClicked
        txtMaloai.setEditable(true);
        txtMota.requestFocusInWindow();
        if(txtMaloai.getText().equals("") || txtMota.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Vui long chon doi tuong de sua");
        }
        flag=2;
        txtMaloai.setEnabled(false);
        btThem.setVisible(true);
        btXoa.setVisible(true);
        btSua.setVisible(true);
        btThoat.setVisible(true);
        btLuu.setVisible(true);
        btHuy.setVisible(true);
    }//GEN-LAST:event_btSuaMouseClicked

    private void btXoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btXoaMouseClicked
        if(txtMaloai.getText().equals(""))
        {
         JOptionPane.showMessageDialog(null, "Vui long chon doi tuong de xoa");   
        }
        else 
        {
        int reply = JOptionPane.showConfirmDialog(null, "Ban co chac xoa khong ?", "Xac nhan xoa", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION)
        {
             Loainentdc delete = new Loainentdc();
             delete.deleteLoaiNen(txtMaloai.getText());
        }
        }
    }//GEN-LAST:event_btXoaMouseClicked

    private void tbLoaiNenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoaiNenMouseClicked
         DefaultTableModel model = (DefaultTableModel) tbLoaiNen.getModel();
        txtMaloai.setText((String) model.getValueAt(tbLoaiNen.getSelectedRow(), 0));
        txtMota.setText((String) model.getValueAt(tbLoaiNen.getSelectedRow(), 1));
    }//GEN-LAST:event_tbLoaiNenMouseClicked

    private void tbLoaiNenPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tbLoaiNenPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_tbLoaiNenPropertyChange

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        loadLoaiNen();
    }//GEN-LAST:event_formWindowActivated

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoaiNen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoaiNen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoaiNen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoaiNen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoaiNen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btHuy;
    private javax.swing.JButton btLuu;
    private javax.swing.JButton btSua;
    private javax.swing.JButton btThem;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbLoaiNen;
    private javax.swing.JTextField txtMaloai;
    private javax.swing.JTextField txtMota;
    // End of variables declaration//GEN-END:variables

}
